from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time

# 登录
def login(driver,users,sleeptime):
    print("开始登录")
    # 打开12306网站
    driver.get("https://www.12306.cn")
    # 设置浏览器全屏
    driver.maximize_window()
    time.sleep(sleeptime)
    # 点击登录
    # driver.find_element_by_link_text("登录").click()
    driver.find_element(By.ID,"J-btn-login").click()
    # driver.find_element(By.XPATH,"//a[@id='J-btn-login']").click()
    time.sleep(sleeptime)
    # 输入用户名
    driver.find_element(By.ID,"J-userName").send_keys(users[0][0])
    # driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]/input").send_keys(users[0][0])
    time.sleep(sleeptime)
    # 输入密码
    driver.find_element(By.ID,"J-password").send_keys(users[0][1])
    # driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div[2]/input").send_keys(users[0][1])
    time.sleep(sleeptime)
    # 点击登录
    driver.find_element(By.ID,"J-login").click()
    time.sleep(sleeptime)

# 登出
def logout(driver):
    driver.find_element(By.XPATH,"//*[@id='J-header-logout']/a[2]").click()
    print("退出登录")

# 验证码
def verificationCode(driver):
    print("验证码")
    time.sleep(2)
    div1 = driver.find_element(By.ID,"nc_1_n1z")
    # print(div1.size['width'])
    div2 = driver.find_element(By.CLASS_NAME ,"nc-lang-cnt")
    # print(div2.size['width'])
    ActionChains(driver).click_and_hold(on_element=div1).perform()

    # time.sleep(0.15)
    # ActionChains(driver).move_to_element_with_offset(to_element=div1, xoffset=100, yoffset=10).perform()
    # time.sleep(0.2)
    # ActionChains(driver).move_to_element_with_offset(to_element=div1, xoffset=150, yoffset=15).perform()
    # time.sleep(0.4)
    # ActionChains(driver).move_to_element_with_offset(to_element=div1, xoffset=120, yoffset=40).release().perform()

    # tr = get_track(div2.size['width']-div1.size['width'])
    # print(tr)
    # for x in tr:
    #     ActionChains(driver).move_to_element_with_offset(to_element=div1, xoffset=x, yoffset=x+10).perform()
    #     time.sleep(0.01)
    # ActionChains(driver).move_to_element_with_offset(to_element=div1, xoffset=1, yoffset=20).release().perform()

    ActionChains(driver).drag_and_drop_by_offset(div1, div2.size['width']-div1.size['width']+10, 0).perform()
    ActionChains(driver).release()
    time.sleep(2)
    # /html/body/div[2]/div[7]/div[2]/div[3]/a
    driver.find_element(By.XPATH,"//a[@class='btn btn-primary ok']").click()
    # driver.find_element(By.XPATH,"//*[@id='pop_163976523482037905']/div[2]/div[3]/a").click()
    # driver.find_element(By.CLASS_NAME,"btn btn-primary ok").click()
    time.sleep(2)

# def get_track(distance):
#     track = []
#     current = 0
#     mid = distance * 3 / 4
#     t = 0.2
#     v = 0
#     while current < distance:
#         if current < mid:
#             a = 2
#         else:
#             a = -3
#         v0 = v
#         v = v0 + a * t
#         move = v0 * t + 1 / 2 * a * t * t
#         current += move
#         track.append(round(move))
#     return track

if __name__ == '__main__':
    print('test')